package utils
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class Checkbutton extends Component
	{
		public static const EVENT:String = "event";
		public var index:int;
		public var value:Boolean = false;
		
		private var foreground:Sprite;
		
		public function Checkbutton(text:String)
		{
			graphics.lineStyle(1, 0x000000);
			graphics.beginFill(0xFFFFFF);
			graphics.drawRect(0, 0, 15, 15);
			graphics.endFill();
			
			var label:Label = new Label(text);
			label.x = 18;
			label.y = -1;
			addChild(label);
			
			foreground = new Sprite();
			foreground.x = foreground.y = 3;
			addChild(foreground);
			addEventListener(MouseEvent.CLICK, mouseClick);
			
			buttonMode = true;
		}
		
		protected function mouseClick(event:MouseEvent = void):void
		{
			setChecked(!value);
			dispatchEvent(new Event(Checkbutton.EVENT));
		}
		
		public function setChecked(check:Boolean):void
		{
			if (check == value) return void;
			if (check == true)
			{
				value = true;
				foreground.graphics.lineStyle(1, 0x000000);
				foreground.graphics.beginFill(0x000000);
				foreground.graphics.drawRect(0, 0, 9, 9);
				foreground.graphics.endFill();
			}
			else
			{
				value = false;
				foreground.graphics.clear();
			}
		}
	}
}